package cn.quicklydoit.resource;

import cn.quicklydoit.entity.User;
import cn.quicklydoit.service.UserService;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
@Path("/user")
@Tag(name = "UserResource",description="用户接口列表")
public class UserResource {

    @Inject
    UserService userService;

    @Context
    HttpHeaders headers;

    public HttpHeaders getHeader() {
        return headers;
    }

    private static final Logger LOG = Logger.getLogger(UserResource.class);

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/list")
    @Operation(summary="获取所有用户",description = "这个接口用户获取所有用户",hidden = false,deprecated = false)
    public Iterable<User> getAll(){
        return userService.getAll();
    }

    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/add")
    @Operation(hidden = true)
    public Response add(User user){
        this.userService.insert(user);
        return Response.status(Response.Status.OK).entity(user).type(MediaType.APPLICATION_JSON).build();
    }

    @POST
    @Path("/login")
    @Operation(summary = "用户登录",description = "用户登录接口")
    public Response login(User user){

        LOG.info("我是info消息："+getHeader().getHeaderString("host"));
        LOG.debug("我是debugger消息："+getHeader().getHeaderString("host"));
        System.out.println("我是system.out.println消息："+getHeader().getHeaderString("host"));
        MultivaluedMap<String, String> requestHeaders = getHeader().getRequestHeaders();
        for(String key:requestHeaders.keySet()){
            System.out.println("key:("+key+") ---- value:("+requestHeaders.get(key)+")");
        }
        return this.userService.login(user);
    }

    @POST
    @Path("/sendValidValues")
    @Operation(summary = "发送验证信息",description = "给用发邮箱发送验证信息")
    public Response validEmail(User user){
        this.userService.sendValidValues(user,"validEmail");
        return Response.status(Response.Status.OK).build();
    }

    @POST
    @Path("/register")
    @Operation(summary = "用户注册",description = "用于用户注册接口")
    public Response register(User user){
        return  this.userService.register(user);
    }

    @POST
    @Path("/isused")
    @Operation(summary = "判重",description = "判断用户是否重复")
    public Response isUsed(User user){
        return Response.status(Response.Status.OK).entity(user).type(MediaType.APPLICATION_JSON).build();
    }
}
